package com.cyys.modules.mobile.wx;

import com.cyys.common.dataScope.DataScope;
import com.cyys.common.ql.AR;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.system.service.thirdInterface.SmsActivateService;
import com.cyys.modules.wx.model.user.WxUserinfo;
import com.cyys.modules.wx.service.user.WxUserinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 微信用户相关接口
 */
@RestController
@RequestMapping("/api/wx/user")
@Api(tags = "微信用户相关接口")
@Validated
public class WxUserAction extends BaseAction {

    @Resource
    private WxUserinfoService wxUserinfoService;

    @Resource
    private SmsActivateService smsActivateService;

    /**
     * 手机号注册
     * @param phone
     * @param password
     * @param openId
     */
    @ApiOperation(value = "用户注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "openId", value = "微信ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token参数", required = true, dataType = "String", paramType = "header")
    })
    @PostMapping("/register")
    public AR register(String phone, String password, String openId) throws Exception {
        if (StringUtils.isBlank(phone)) {
            return AR.failed("缺少参数【phone】");
        }
        if (StringUtils.isBlank(password)) {
            return AR.failed("缺少参数【password】");
        }
        return wxUserinfoService.register(phone, password, openId);
    }

    @ApiOperation(value = "用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openId", value = "微信openId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token参数", required = true, dataType = "String", paramType = "header")
    })
    @PostMapping("/login")
    public AR login(String phone, String password, String openId) throws Exception {
        if (StringUtils.isBlank(phone)) {
            return AR.failed("缺少参数【phone】");
        }
        if (StringUtils.isBlank(openId)) {
            return AR.failed("缺少参数【openId】");
        }
        if (StringUtils.isBlank(password)) {
            return AR.failed("缺少参数【password】");
        }
        return wxUserinfoService.login(phone, password, openId);
    }

    /**
     * 微信手机号一键注册
     * @param openId 微信ID
     * @param encryptDataB64
     * @param sessionKeyB64
     * @param ivB64
     */
    @ApiOperation(value = "微信手机号一键注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openId", value = "微信ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "encryptDataB64", value = "加密数据", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sessionKeyB64", value = "session数据", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ivB64", value = "偏移量", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token参数", required = true, dataType = "String", paramType = "header")
    })
    @PostMapping("/register/registerByRealPhone")
    public AR registerByRealPhone(String openId, String encryptDataB64, String sessionKeyB64, String ivB64) throws Exception {
        return wxUserinfoService.registerByRealPhone(openId, encryptDataB64, sessionKeyB64, ivB64);
    }

    /**
     * 微信手机号一键登录
     * @param openId
     * @param encryptDataB64
     * @param sessionKeyB64
     * @param ivB64
     */
    @ApiOperation(value = "微信手机号一键登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openId", value = "微信ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "encryptDataB64", value = "加密数据", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sessionKeyB64", value = "session数据", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ivB64", value = "偏移量", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token参数", required = true, dataType = "String", paramType = "header")
    })
    @PostMapping("/login/loginByRealPhone")
    public AR loginByRealPhone(String openId, String encryptDataB64, String sessionKeyB64, String ivB64) throws Exception {
         return  wxUserinfoService.loginByRealPhone(openId,encryptDataB64,sessionKeyB64,ivB64);
    }

    @ApiOperation(value = "修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "oldPassword", value = "原始密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token参数", required = true, dataType = "String", paramType = "header")
    })
    @PostMapping("/modify/modifyPwd")
    public AR modifyPwd(String userId, String password, String oldPassword) throws Exception {
        if (StringUtils.isBlank(userId)) {
            return AR.failed("缺少参数【userId】");
        }
        if (StringUtils.isBlank(password)) {
            return AR.failed("缺少参数【password】");
        }
        if (StringUtils.isBlank(oldPassword)) {
            return AR.failed("缺少参数【oldPassword】");
        }
        return wxUserinfoService.modifyPwd(userId, password, oldPassword);
    }

    @ApiOperation(value = "发送短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token参数", required = true, dataType = "String", paramType = "header")
    })
    @PostMapping("/modify/sendVerificationCode")
    public AR sendVerificationCode(String phone) throws Exception {
        if (StringUtils.isBlank(phone)) {
            return AR.failed("缺少参数【phone】");
        }
        WxUserinfo user = wxUserinfoService.getByPhone(phone);
        if (user == null) {
            return AR.failed(1004, "手机号未注册");
        }
        return smsActivateService.sendActivateCode(phone, "短信模板编号", 10);
    }

    @ApiOperation(value = "验证短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token参数", required = true, dataType = "String", paramType = "header")
    })
    @PostMapping("/modify/judgeActivateCode")
    public AR judgeActivateCode(String phone, String code) throws Exception {
        if (StringUtils.isBlank(phone)) {
            AR.failed("缺少参数【phone】");
        } else if (StringUtils.isBlank(code)) {
            AR.failed("缺少参数【code】");
        }
        return smsActivateService.judgeActivateCode(phone, code);
    }

    @ApiOperation(value = "（忘记密码后）重新设置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token参数", required = true, dataType = "String", paramType = "header")
    })
    @PostMapping("/modify/findBackPassword")
    public AR findBackPassword(String phone, String password) throws Exception {
        if (StringUtils.isBlank(phone)) {
            return AR.failed("缺少参数【phone】");
        } else if (StringUtils.isBlank(password)) {
            return AR.failed("缺少参数【password】");
        }
        return wxUserinfoService.findBackPassword(phone, password);
    }

    /**
     * @param userId
     * @param openId
     * @param password
     */
    @ApiOperation(value = "新用户设置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "openId", value = "微信id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token参数", required = true, dataType = "String", paramType = "header")
    })
    @PostMapping("/modify/setPassword")
    public AR setPassword(String userId, String openId, String password)throws Exception{
        if(StringUtils.isBlank(userId)){
            return AR.failed("缺少参数【userId】");
        }else if(StringUtils.isBlank(password)){
            return AR.failed("缺少参数【password】");
        }else if(StringUtils.isBlank(openId)){
            return AR.failed("缺少参数【openid】");
        }
        return wxUserinfoService.setPassword(userId, openId, password);
    }

    @ApiOperation(value = "我的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token参数", required = true, dataType = "String", paramType = "header")
    })
    @PostMapping("/getUserInfo")
    public AR getUserInfo(String userId) throws Exception {
        if (StringUtils.isBlank(userId)) {
            return AR.failed("缺少参数【phone】");
        }
        WxUserinfo wxUserinfo = wxUserinfoService.getById(userId);
        if (wxUserinfo == null) {
            AR.failed("用户信息获取失败");
        }
        return AR.ok(wxUserinfo.toResponseEntity());
    }
}
